﻿using System.Collections.Generic;
using System.Data;

namespace ExtensionsLib.Dataset
{

    public static class DataRowExtensions
    {


        /// <summary>
        /// Toes the self. 
        /// </summary>
        /// <param name="dr">The dr.</param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this DataRow dr)
        {
            IDictionary<string, string> result = new Dictionary<string, string>();
            foreach (DataColumn column in dr.Table.Columns)
            {
                string name = column.ColumnName;
                string value = dr[name].Cast<string>();
                result.Add(name, value);
            }
            return result;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <param name="columnName"></param>
        /// <param name="_default"></param>
        /// <returns></returns>
        public static T Field<T>(this DataRow row, string columnName, T _default)
        {
            if (row.Table.Columns.Contains(columnName))
            {
                return row[columnName].Cast<T>();
            }
            else
            {
                return (T)_default;
            }
        }



    }
}


